package com.lv.common.dialog

import android.content.Context
import android.view.Gravity
import android.widget.ImageView
import androidx.viewpager2.widget.ViewPager2
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.luck.picture.lib.photoview.PhotoView
import com.lv.common.R
import com.lv.common.base.BaseDialog
import com.lv.common.imp.OnDialogSureListener

/**
 * 作者： ygx
 * 创建日期：2022/5/18 9:53 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * _              _           _     _   ____  _             _ _
 * / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 */
class ImageSampleDialog : BaseDialog {

    private var ivClose: ImageView? = null
    private var mViewPager: ViewPager2? = null
    private val mUrls = mutableListOf<String>()
    private var mContext: Context? = null
    private var mPosition=0
    private var mAdapter: BaseQuickAdapter<String, BaseViewHolder>? = null
//    constructor(mContext: Context?) : super(
//        mContext,
//        false,
//        false
//    ) {
//    }

    constructor(
        context: Context?,
        urls: List<String>?,
        position:Int=0,
    ) : super(context, false, true, Gravity.CENTER, true, true) {
        urls?.let {
            mUrls.clear()
            mUrls.addAll(urls)
        }
        mPosition=position
        mContext = context
    }

    constructor(
        context: Context?,
        isCancelable: Boolean,
        isBackCancelable: Boolean,
        gravity: Int,
        widthIsMatch: Boolean,
        heightIsMatch: Boolean
    ) : super(context, isCancelable, isBackCancelable, gravity, widthIsMatch, heightIsMatch) {
        mContext = context
    }

    override fun initView() {

        ivClose = findViewById(R.id.iv_close)
        mViewPager = findViewById(R.id.viewPager)
        initViewPager()

        initEvnet()
    }

    private fun initViewPager() {
        mAdapter = object : BaseQuickAdapter<String, BaseViewHolder>
            (R.layout.item_pic_scan_dialog, mUrls) {
            override fun convert(holder: BaseViewHolder, item: String) {

//                val mClContainer: ConstraintLayout = holder.getView<ConstraintLayout>(R.id.cl_container)
                val mIvPhoto: PhotoView = holder.getView<PhotoView>(R.id.iv_photo)
                val options = RequestOptions()
                    .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                    .format(DecodeFormat.PREFER_RGB_565)
                Glide.with(context).load(item).apply(options).into(mIvPhoto)
//                mClContainer.setOnClickListener {
//                    dismiss()
//                }
            }
        }
        mViewPager?.adapter = mAdapter
        mViewPager?.currentItem=mPosition
    }

    private fun initEvnet() {

        findViewById<ImageView>(R.id.iv_close)?.setOnClickListener { dismiss() }
    }

    override fun getLayoutId(): Int {
        return R.layout.dialog_image_smaple
    }

    private var onDialogSureListener: OnDialogSureListener? = null

}